package go_sfinti

import (
	"encoding/json"
	"gitee.com/wms3001/goSFInti/model"
	"gitee.com/wms3001/goSFInti/tool"
	"github.com/go-resty/resty/v2"
	"io"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type SFInti struct {
	Url     string `json:"url"`
	Key     string `json:"key"`
	Secret  string `json:"secret"`
	AesCode string `json:"aesCode"`
	Token   string `json:"token"`
	MsgType string `json:"msgType"`
	Nonce   string `json:"nonce"`
}

func init() {
	log.SetFlags(log.Llongfile | log.LstdFlags)
}

// 获取token
func (sFInti *SFInti) GetToken() model.SFToken {
	sFToken := model.SFToken{}
	client := resty.New()
	resp, err := client.R().
		EnableTrace().
		Get(sFInti.Url + ".global/openapi/api/token?appKey=" + sFInti.Key + "&appSecret=" + sFInti.Secret)
	if err != nil {
		sFToken.ApiResultCode = -1
		sFToken.ApiErrorMsg = err.Error()
	} else {
		json.Unmarshal([]byte(resp.Body()), &sFToken)
	}
	return sFToken
}

// 电商下单
func (sFInti *SFInti) IECSOrder(order model.SFIECSOrder) model.SFIECSOrderResponse {
	sFIECSOrderResponse := model.SFIECSOrderResponse{}
	sFResponse := model.SFResponse{}
	timestamp := strconv.FormatInt(time.Now().UnixMicro(), 10)
	ostr, _ := json.Marshal(order)
	param := string(ostr)

	p, err := sFInti.GetSign(param, timestamp)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderResponse.SFResponse = sFResponse
		return sFIECSOrderResponse
	}
	res, err := sFInti.Send(p, "POST", timestamp, "IECS_CREATE_ORDER")
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderResponse.SFResponse = sFResponse
		return sFIECSOrderResponse
	}
	json.Unmarshal([]byte(res), &sFResponse)
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderResponse.SFResponse = sFResponse
		return sFIECSOrderResponse
	}
	sData := model.SFIECSOrderDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIECSOrderResponse.SFResponse = sFResponse
	sFIECSOrderResponse.DeData = sData
	return sFIECSOrderResponse
}

// 电商下面单
func (sFInti *SFInti) IECSOrderLabel(req model.SFIECSOrderLabel) model.SFIECSOrderLabelResponse {
	sFIECSOrderLabelResponse := model.SFIECSOrderLabelResponse{}
	sFResponse := model.SFResponse{}
	timestamp := strconv.FormatInt(time.Now().UnixMicro(), 10)
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	p, err := sFInti.GetSign(param, timestamp)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderLabelResponse.SFResponse = sFResponse
		return sFIECSOrderLabelResponse
	}
	res, err := sFInti.Send(p, "POST", timestamp, "IECS_PRINT_ORDER")
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderLabelResponse.SFResponse = sFResponse
		return sFIECSOrderLabelResponse
	}
	json.Unmarshal([]byte(res), &sFResponse)
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderLabelResponse.SFResponse = sFResponse
		return sFIECSOrderLabelResponse
	}
	sData := model.SFIECSOrderLabelDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIECSOrderLabelResponse.SFResponse = sFResponse
	sFIECSOrderLabelResponse.DeData = sData
	return sFIECSOrderLabelResponse
}

// 电商运费查询
func (sFInti *SFInti) SFIECSOrderFee(req model.SFIECSOrderFee) model.SFIECSOrderFeeResponse {
	sFIECSOrderFeeResponse := model.SFIECSOrderFeeResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IECS_QUERY_WEIGHT_FREIGHT")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderFeeResponse.SFResponse = sFResponse
		return sFIECSOrderFeeResponse
	}
	sData := model.SFIECSOrderFeeDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIECSOrderFeeResponse.SFResponse = sFResponse
	sFIECSOrderFeeResponse.DeData = sData
	return sFIECSOrderFeeResponse
}

// 电商拦截订单
func (sFInti *SFInti) SFIECSOrderIntercept(req model.SFIECSOrderIntercept) model.SFIECSOrderInterceptResponse {
	sFIECSOrderInterceptResponse := model.SFIECSOrderInterceptResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IECS_ORDER_INTERCEPT")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderInterceptResponse.SFResponse = sFResponse
		return sFIECSOrderInterceptResponse
	}
	sData := model.SFIECSOrderInterceptDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIECSOrderInterceptResponse.SFResponse = sFResponse
	sFIECSOrderInterceptResponse.DeData = sData
	return sFIECSOrderInterceptResponse
}

// 电商取消订单
func (sFInti *SFInti) SFIECSOrderCancel(req model.SFIECSOrderCancel) model.SFIECSOrderCancelResponse {
	sFIECSOrderCancelResponse := model.SFIECSOrderCancelResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IECS_CANCEL_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderCancelResponse.SFResponse = sFResponse
		return sFIECSOrderCancelResponse
	}
	sData := model.SFIECSOrderCancelDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIECSOrderCancelResponse.SFResponse = sFResponse
	sFIECSOrderCancelResponse.DeData = sData
	return sFIECSOrderCancelResponse
}

// 获取电商订单运费重量
func (sFInti *SFInti) SFIECSOrderUploadWeight(req model.SFIECSOrderUploadWeight) model.SFIECSOrderUploadWeightResponse {
	sFIECSOrderUploadWeightResponse := model.SFIECSOrderUploadWeightResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IECS_UPLOAD_WEIGHT")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderUploadWeightResponse.SFResponse = sFResponse
		return sFIECSOrderUploadWeightResponse
	}
	sData := model.SFIECSOrderUploadWeightDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIECSOrderUploadWeightResponse.SFResponse = sFResponse
	sFIECSOrderUploadWeightResponse.DeData = sData
	return sFIECSOrderUploadWeightResponse
}

// 电商获取服务商单号
func (sFInti *SFInti) SFIECSOrderAgentno(req model.SFIECSOrderAgentno) model.SFIECSOrderAgentnoResponse {
	sFIECSOrderAgentnoResponse := model.SFIECSOrderAgentnoResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IECS_QUERY_AGENTNO")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSOrderAgentnoResponse.SFResponse = sFResponse
		return sFIECSOrderAgentnoResponse
	}
	sData := model.SFIECSOrderAgentnoDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIECSOrderAgentnoResponse.SFResponse = sFResponse
	sFIECSOrderAgentnoResponse.DeData = sData
	return sFIECSOrderAgentnoResponse
}

// 电商包裹绑定订单
func (sFInti *SFInti) SFIECSPackageBindOrder(req model.SFIECSPackageBindOrder) model.SFIECSPackageBindOrderResponse {
	sFIECSPackageBindOrderResponse := model.SFIECSPackageBindOrderResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IECS_QUERY_AGENTNO")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIECSPackageBindOrderResponse.SFResponse = sFResponse
		return sFIECSPackageBindOrderResponse
	}
	sData := model.SFIECSPackageBindOrderDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIECSPackageBindOrderResponse.SFResponse = sFResponse
	sFIECSPackageBindOrderResponse.DeData = sData
	return sFIECSPackageBindOrderResponse
}

// 快递下单
func (sFInti *SFInti) SFIUOPOrder(req model.SFIUOPOrder) model.SFIUOPOrderResponse {
	sFIUOPOrderResponse := model.SFIUOPOrderResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_CREATE_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPOrderResponse.SFResponse = sFResponse
		return sFIUOPOrderResponse
	}
	sData := model.SFIUOPOrderDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPOrderResponse.SFResponse = sFResponse
	sFIUOPOrderResponse.DeData = sData
	return sFIUOPOrderResponse
}

// 快递下载面单
func (sFInti *SFInti) SFIUOPOrderLabel(req model.SFIUOPOrderLabel) model.SFIUOPOrderLabelResponse {
	sFIUOPOrderLabelResponse := model.SFIUOPOrderLabelResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_PRINT_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPOrderLabelResponse.SFResponse = sFResponse
		return sFIUOPOrderLabelResponse
	}
	sData := model.SFIUOPOrderLabelDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPOrderLabelResponse.SFResponse = sFResponse
	sFIUOPOrderLabelResponse.DeData = sData
	return sFIUOPOrderLabelResponse
}

// 快递订单查询
func (sFInti *SFInti) SFIUOPOrderQuery(req model.SFIUOPOrderQuery) model.SFIUOPOrderQueryResponse {
	SFIUOPOrderQueryResponse := model.SFIUOPOrderQueryResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_QUERY_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		SFIUOPOrderQueryResponse.SFResponse = sFResponse
		return SFIUOPOrderQueryResponse
	}
	sData := model.SFIUOPOrderQueryDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	SFIUOPOrderQueryResponse.SFResponse = sFResponse
	SFIUOPOrderQueryResponse.DeData = sData
	return SFIUOPOrderQueryResponse
}

// 快递查询订单是否需要上传证件
func (sFInti *SFInti) SFIUOPOrderRequireCertify(req model.SFIUOPOrderRequireCertify) model.SFIUOPOrderRequireCertifyResponse {
	sFIUOPOrderRequireCertifyResponse := model.SFIUOPOrderRequireCertifyResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_REQUIRE_CERTIFY")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPOrderRequireCertifyResponse.SFResponse = sFResponse
		return sFIUOPOrderRequireCertifyResponse
	}
	sData := model.SFIUOPOrderRequireCertifyDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPOrderRequireCertifyResponse.SFResponse = sFResponse
	sFIUOPOrderRequireCertifyResponse.DeData = sData
	return sFIUOPOrderRequireCertifyResponse
}

// 快递上传身份证明文件
func (sFInti *SFInti) SFIUOPOrderUploadCertify(req model.SFIUOPOrderUploadCertify) model.SFIUOPOrderUploadCertifyResponse {
	sFIUOPOrderUploadCertifyResponse := model.SFIUOPOrderUploadCertifyResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_UPLOAD_CERTIFY")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPOrderUploadCertifyResponse.SFResponse = sFResponse
		return sFIUOPOrderUploadCertifyResponse
	}
	sData := model.SFIUOPOrderUploadCertifyDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPOrderUploadCertifyResponse.SFResponse = sFResponse
	sFIUOPOrderUploadCertifyResponse.DeData = sData
	return sFIUOPOrderUploadCertifyResponse
}

// 快递身份验证
func (sFInti *SFInti) IUOPValidityCertify(req model.IUOPValidityCertify) model.IUOPValidityCertifyResponse {
	iUOPValidityCertifyResponse := model.IUOPValidityCertifyResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_UPLOAD_CERTIFY")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		iUOPValidityCertifyResponse.SFResponse = sFResponse
		return iUOPValidityCertifyResponse
	}
	sData := model.IUOPValidityCertifyDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	iUOPValidityCertifyResponse.SFResponse = sFResponse
	iUOPValidityCertifyResponse.DeData = sData
	return iUOPValidityCertifyResponse
}

// 快递取消订单
func (sFInti *SFInti) SFIUOPOrderCancel(req model.SFIUOPOrderCancel) model.SFIUOPOrderCancelResponse {
	sFIUOPOrderCancelResponse := model.SFIUOPOrderCancelResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_CANCEL_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPOrderCancelResponse.SFResponse = sFResponse
		return sFIUOPOrderCancelResponse
	}
	sData := model.SFIUOPOrderCancelDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPOrderCancelResponse.SFResponse = sFResponse
	sFIUOPOrderCancelResponse.DeData = sData
	return sFIUOPOrderCancelResponse
}

// 快递获取轨迹
func (sFInti *SFInti) SFIUOPQueryTrack(req model.SFIUOPQueryTrack) model.SFIUOPQueryTrackResponse {
	sFIUOPQueryTrackResponse := model.SFIUOPQueryTrackResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_CANCEL_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPQueryTrackResponse.SFResponse = sFResponse
		return sFIUOPQueryTrackResponse
	}
	sData := model.SFIUOPQueryTrackDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPQueryTrackResponse.SFResponse = sFResponse
	sFIUOPQueryTrackResponse.DeData = sData
	return sFIUOPQueryTrackResponse
}

// 快递预合单
func (sFInti *SFInti) SFIUOPBBDPreMerge(req model.SFIUOPBBDPreMerge) model.SFIUOPBBDPreMergeResponse {
	sFIUOPBBDPreMergeResponse := model.SFIUOPBBDPreMergeResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_BBD_PRE_MERGE")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPBBDPreMergeResponse.SFResponse = sFResponse
		return sFIUOPBBDPreMergeResponse
	}
	sData := model.SFIUOPBBDPreMergeDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPBBDPreMergeResponse.SFResponse = sFResponse
	sFIUOPBBDPreMergeResponse.DeData = sData
	return sFIUOPBBDPreMergeResponse
}

// 快递取消预合单
func (sFInti *SFInti) SFIUOPBBDCancelMerge(req model.SFIUOPBBDCancelMerge) model.SFIUOPBBDCancelMergeResponse {
	sFIUOPBBDCancelMergeResponse := model.SFIUOPBBDCancelMergeResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_BBD_PRE_MERGE")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPBBDCancelMergeResponse.SFResponse = sFResponse
		return sFIUOPBBDCancelMergeResponse
	}
	sData := model.SFIUOPBBDCancelMergeDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPBBDCancelMergeResponse.SFResponse = sFResponse
	sFIUOPBBDCancelMergeResponse.DeData = sData
	return sFIUOPBBDCancelMergeResponse
}

// 快递查询预合单结果
func (sFInti *SFInti) SFIUOPBBDMergeResult(req model.SFIUOPBBDMergeResult) model.SFIUOPBBDMergeResultResponse {
	sFIUOPBBDMergeResultResponse := model.SFIUOPBBDMergeResultResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_BBD_PRE_MERGE")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPBBDMergeResultResponse.SFResponse = sFResponse
		return sFIUOPBBDMergeResultResponse
	}
	sData := model.SFIUOPBBDMergeResultDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPBBDMergeResultResponse.SFResponse = sFResponse
	sFIUOPBBDMergeResultResponse.DeData = sData
	return sFIUOPBBDMergeResultResponse
}

// 快递单面单异步打印
func (sFInti *SFInti) SFIUOPOrderAsynLabel(req model.SFIUOPOrderAsynLabel) model.SFIUOPOrderAsynLabelResponse {
	sFIUOPOrderAsynLabelResponse := model.SFIUOPOrderAsynLabelResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_ASYN_PRINT_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPOrderAsynLabelResponse.SFResponse = sFResponse
		return sFIUOPOrderAsynLabelResponse
	}
	sData := model.SFIUOPOrderAsynLabelDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPOrderAsynLabelResponse.SFResponse = sFResponse
	sFIUOPOrderAsynLabelResponse.DeData = sData
	return sFIUOPOrderAsynLabelResponse
}

// 快递查询面单异步打印结果
func (sFInti *SFInti) SFIUOPOrderAsynPrintResult(req model.SFIUOPOrderAsynPrintResult) model.SFIUOPOrderAsynPrintResultResponse {
	sFIUOPOrderAsynPrintResultResponse := model.SFIUOPOrderAsynPrintResultResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_ASYN_PRINT_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPOrderAsynPrintResultResponse.SFResponse = sFResponse
		return sFIUOPOrderAsynPrintResultResponse
	}
	sData := model.SFIUOPOrderAsynPrintResultDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPOrderAsynPrintResultResponse.SFResponse = sFResponse
	sFIUOPOrderAsynPrintResultResponse.DeData = sData
	return sFIUOPOrderAsynPrintResultResponse
}

func (sFInti *SFInti) SFIUOPQueryFreight(req model.SFIUOPQueryFreight) model.SFIUOPQueryFreightResponse {
	sFIUOPQueryFreightResponse := model.SFIUOPQueryFreightResponse{}
	ostr, _ := json.Marshal(req)
	param := string(ostr)
	sFResponse := sFInti.Request(param, "IUOP_ASYN_PRINT_ORDER")
	ttt, err := sFInti.DecodeMsg(sFResponse.ApiResultData)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		sFIUOPQueryFreightResponse.SFResponse = sFResponse
		return sFIUOPQueryFreightResponse
	}
	sData := model.SFIUOPQueryFreightDeData{}
	json.Unmarshal([]byte(ttt), &sData)
	sFIUOPQueryFreightResponse.SFResponse = sFResponse
	sFIUOPQueryFreightResponse.DeData = sData
	return sFIUOPQueryFreightResponse
}

func (sFInti *SFInti) Request(param string, msgType string) model.SFResponse {
	timestamp := strconv.FormatInt(time.Now().UnixMicro(), 10)
	sFResponse := model.SFResponse{}
	p, err := sFInti.GetSign(param, timestamp)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		return sFResponse
	}
	res, err := sFInti.Send(p, "POST", timestamp, msgType)
	if err != nil {
		sFResponse.ApiResultCode = -1
		sFResponse.ApiErrorMsg = err.Error()
		return sFResponse
	}
	json.Unmarshal([]byte(res), &sFResponse)
	return sFResponse
}

// 消息加密获取签名
func (sFInti *SFInti) GetSign(param string, timestamp string) (tool.Param, error) {
	biz, err := tool.NewBizMsgCrypt(sFInti.Token, sFInti.AesCode, sFInti.Key)
	if err != nil {
		return tool.Param{}, err
	}
	p := biz.EncryptMsg(param, timestamp, sFInti.Nonce)
	return p, nil
}

// 解密消息
func (sFInti *SFInti) DecodeMsg(response string) (string, error) {
	biz, err := tool.NewBizMsgCrypt(sFInti.Token, sFInti.AesCode, sFInti.Key)
	if err != nil {
		return "", err
	}
	return biz.DecryptMsg(response)
}

// 发送请求
func (sFInti *SFInti) Send(p tool.Param, method string, timestamp string, msgType string) (string, error) {
	url := sFInti.Url + ".global/openapi/api/dispatch"
	client1 := &http.Client{}
	payload := strings.NewReader(p.Encrypt)
	req, err := http.NewRequest(method, url, payload)
	if err != nil {
		return "", err
	}
	req.Header.Add("appKey", sFInti.Key)
	req.Header.Add("token", sFInti.Token)
	req.Header.Add("timestamp", timestamp)
	req.Header.Add("nonce", sFInti.Nonce)
	req.Header.Add("signature", p.Signature)
	req.Header.Add("msgType", msgType)
	req.Header.Add("lang", "zh-CN")
	req.Header.Add("Content-Type", "application/json")

	res, err := client1.Do(req)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		return "", err
	}
	return string(body), nil
}
